<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Rss_getter extends CI_Controller {
	var $user_cookie;
	function __construct()
	{
		parent::__construct();
		$this->load->database();
		global $user_cookie;
		$user_cookie = $this->input->cookie('bv_user_cookie');
		if ($user_cookie == "") {
			$cookie = array(
					'name'   => 'user_cookie',
					'value'  => md5(rand(1, 100000)),
					'expire' => time() + 86500,
					'domain' => '',
					'path'   => '/',
					'prefix' => 'bv_',
					'secure' => FALSE
			);

			$this->input->set_cookie($cookie);
		}
		
		//Check if there is exists user with this cookie
		$exists_user = $this->db->query("SELECT * FROM user WHERE user_cookie = '$user_cookie'");
		if ($exists_user->num_rows() > 0) {
			//load setting for this user
			if ($exists_user->row(0)->user_username == "") {
			//If user had had an account.
			
			} else {
			//Anonymous user, give them a new cookie

			}
		} else {
		//First time user, give them new cookir - start to auto analyst
			$this->db->query("INSERT INTO  `user` (`user_id` ,`user_username` ,`user_password` ,`user_email` ,`user_cookie`) VALUES (NULL ,  '',  '',  '',  '$user_cookie');");
		}
	}
	public function index() {
	
	}
	
	public function whoiam() {
	
		global $user_cookie;
		
		//$data['data'] = $this->input->cookie('bv_user_cookie');
		$data['data'] = $user_cookie;
		$this->load->view('test_view', $data);
	}
	public function get_feeds() //Run by cronjob or schedule.
	{
		die("TEST");
		$dropall = $this->db->query("TRUNCATE TABLE  `feed`");
		
		$done = array();
		$all_category = $this->db->query("SELECT * FROM category");
		
		foreach ($all_category->result() as $row) {
		//for ($i = 1; $i <= 4; $i++) {
			$i = $row->cat_id;
			
			$query = $this->db->query("SELECT rsslink_id, source_id, rsslink_url FROM rsslink WHERE cat_id = $i AND rsslink_id = rsslink_id;");
			
			foreach ($query->result() as $row2) {
				$link = $row2->rsslink_url;
				$source_id = $row2->source_id;
				
				$rss = new DOMDocument();
				$rss->load($link);
				
				$feed = array();
				foreach ($rss->getElementsByTagName('item') as $node) {
					$item = array ( 
						'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
						'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
						'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
						'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
						);
					array_push($feed, $item);
				}
				
				foreach ($feed as $f) {
					$query = $this->db->query("INSERT INTO  `feed` (`feed_id` ,`source_id` ,`feed_excerpt` ,`feed_title` ,`feed_thumbnail`, `feed_link`, `feed_date`, `feed_cat`)VALUES (NULL ,  '".$source_id."',  '".(urlencode($f['description']))."',  '".(urlencode($f['title']))."',  '', '".(urlencode($f['link']))."', '".(urlencode($f['pubDate']))."', '$i')");
				}
			}
			$done[] = $i;
		}
		
		$data['data'] = $done;
		$this->load->view('test_view', $data);
	}
	public function view_data() {
			//Moved to homepage
	}
}

/* End of file rss_getter.php */
/* Location: ./application/controllers/rss_getter.php */